package cn.itrip.auth.service;

import cn.itrip.dao.ItripUserMapper;
import cn.itrip.pojo.ItripUser;
import cn.itrip.utlis.MD5Utils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class UserLogServiceImpl implements UserLogService{

    @Resource
    private ItripUserMapper itripUserMapper;

    /**
     * 验证参数，并查询用户
     * @param userCode
     * @param userPassword
     * @return
     * @throws Exception
     */
    @Override
    public ItripUser userLogin(String userCode, String userPassword) throws Exception {
        ItripUser itripUser = itripUserMapper.selectByUserCode(userCode);
        if(userCode == null && userPassword == null){
            throw new Exception("参数不正确!");
        }
        if(itripUser == null){
            throw new Exception("用户名不存在!");
        }
        if(itripUser.getActivated() != 1){
            throw new Exception("用户未激活!");
        }
        if(!itripUser.getUserpassword().equals(MD5Utils.getMD5(userPassword,32))){
            throw new Exception("密码不正确!");
        }
        return itripUser;
    }

    /**
     * 用户登录
     * @param userCode
     * @return
     * @throws Exception
     */
    @Override
    public ItripUser userLogin(String userCode) throws Exception {

        ItripUser itripUser = itripUserMapper.selectByUserCode(userCode);

        return itripUser;
    }

    /**
     * 保存用户
     * @param user
     * @return
     */
    @Override
    public Boolean saveUser(ItripUser user) {
        return itripUserMapper.insertSelective(user)>0;
    }
}
